//
//  RecommendViewController.m
//  SportGo
//
//  Created by rimi on 16/9/20.
//  Copyright © 2016年 Yrzteam. All rights reserved.
//

#import "RecommendViewController.h"
#import "RzRecommendCell.h"
#import "MBHUDDIY.h"
#import "CycleScrollView.h"
#import "RZDetailViewController.h"
#import "RZAnimationIndicator.h"
@interface RecommendViewController ()<UITableViewDelegate,UITableViewDataSource,CycleScrollViewDelegate,CycleScrollViewDatasource>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataSource;
@property (nonatomic, strong) CycleScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray *imageArray;
@property (nonatomic, strong) RZAnimationIndicator *animationIndicator;
@end

@implementation RecommendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"推荐";
    _imageArray = [NSMutableArray array];
    self.automaticallyAdjustsScrollViewInsets = NO;
   self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    self.animationIndicator = [[RZAnimationIndicator alloc]initWithFrame:CGRectMake(self.view.frame.size.width/2-75, self.view.frame.size.height/2-75, 150, 150)];
    [self.animationIndicator setLoadText:@"正在加载..."];
    [self.view addSubview:self.animationIndicator];
    self.animationIndicator.userInteractionEnabled = NO;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    tap.numberOfTapsRequired = 1;//最多点击2次
    [self.animationIndicator addGestureRecognizer:tap];
    _scrollView = [[CycleScrollView alloc]initWithFrame:CGRectMake(0, 0, screen_size.width, 170)];
    
    _scrollView.animationDuration = 4;
    
    self.tableView.tableHeaderView = _scrollView;
    [self loadNetWorking];
    


   
}

//网络请求方法
- (void)loadNetWorking{
    [self.animationIndicator startAnimation];
    AVQuery *query = [AVQuery queryWithClassName:@"TestObject"];
    [query whereKey:@"objectId" equalTo:@"57e9134d128fe10057176126"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (error) {
            dispatch_async(dispatch_get_main_queue(), ^{
                
                self.tableView.hidden = YES;
                [self.animationIndicator stopAnimationWithLoadText:@"加载失败" withType:NO];//加载失败
                self.animationIndicator.userInteractionEnabled = YES;
            });
           
        }else{
                self.dataSource = [objects[0][@"healthy"][@"source"] mutableCopy];
                for (NSDictionary *dic in objects[0][@"healthy"][@"source"]) {
                    NSURL *url = [NSURL URLWithString:dic[@"headUrl"]];
                    [_imageArray addObject:url];
                }
            
                _scrollView.datasource = self;
                _scrollView.delegate = self;
            
                dispatch_async(dispatch_get_main_queue(), ^{
                  [self.animationIndicator stopAnimationWithLoadText:@"finish" withType:YES];
                   
                [self.tableView reloadData];
                });
        }
    }];
}
- (void)refuresh{
    
}
- (void)tap{
    [self loadNetWorking];

}

- (NSInteger)numberOfPages
{
    return _imageArray.count;
}

- (UIView *)pageAtIndex:(NSInteger)index size:(CGSize)size
{

    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, _scrollView.frame.size.width , _scrollView.frame.size.height)];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    [imageView sd_setImageWithURL:_imageArray[index]];
    return imageView;
}

- (void)scrollView:(CycleScrollView *)scrollView didClickPage:(UIView *)view atIndex:(NSInteger)index
{
        RZDetailViewController *rzDVc = [[RZDetailViewController alloc]initWithDic:self.dataSource[index]];
           [self.navigationController pushViewController:rzDVc animated:YES];

}
#pragma mark --- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RzRecommendCell *cell = [tableView dequeueReusableCellWithIdentifier:@"rzcell"];
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    cell.dic = self.dataSource[indexPath.row];

    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  
    RZDetailViewController *rzDVc = [[RZDetailViewController alloc]initWithDic:self.dataSource[indexPath.row]];

    [self.navigationController pushViewController:rzDVc animated:YES];
       
  


}
#pragma mark -- lazy加载
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,64,screen_size.width, screen_size.height-64-48)];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.rowHeight = 100;
        _tableView.separatorColor = [UIColor clearColor];
        [_tableView registerClass:[RzRecommendCell class] forCellReuseIdentifier:@"rzcell"];
    }

    return _tableView;
}
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}


@end
